#!/usr/bin/env python 
# -*- coding:utf-8 -*-
"""
@Time:2021/11/18 10:00 下午"
@Author:lydia_liu"
@File:guess_number.py
@function:
"""
import random

"""
选择一个【1，10】范围内的随机整数作为目标数字
提示用户输入一个1-10之间的整数
判断用户输入的数字是否为与目标数字相等
如果猜对了，输出猜对啦
如果猜错了，输出猜错了
最后输出游戏结束

"""
#
# compter_num = random.randint(1,10)
# people_num = input("请输入1-10之间的整数：")
# if int(people_num) == compter_num:
#     print("猜对啦")
# else:
#     print(f"猜错啦,正确答案是{compter_num}")
# print("游戏结束！")



"""
猜数字游戏升级
判断用户输入内容的类型
提示用户猜测的数字与目标数字的大小关系
给3次猜数字的机会
"""

# compter_num = random.randint(1,10)
# guess_num = 3
# for i in range(guess_num):
#     while True:
#         people_num = input("请输入1-10之间的整数：")
#         if people_num.isdigit():
#             people_num = int(people_num)
#             break
#         else:
#             print("输入的不是整数！请重新输入！")
#         #判断两个值是否相等
#     if int(people_num) == compter_num:
#         print("猜对了")
#         break
#     else:
#         #如果猜错了，前几次提示提醒信息
#         if i != guess_num - 1:
#             if people_num > compter_num:
#                 print("猜大了")
#             else:
#                 print("猜小了")
#         #最后一次机会还是猜错了，展示正确答案！
#         else:
#             print(f"猜错了，正确答案是：{compter_num}")
# print("游戏结束")

def people_input_num(input_msg,err_msg):
    """
    定义用户输入整数的方法
    :param input_msg: 输入时的提示信息
    :param err_msg: 用户输入后的错误提示信息
    :return: 转化为整数类型的数字
    """
    while True:
        people_num = input(input_msg)
        #判断用户输入的内容类型
        if people_num.isdigit():
            people_num = int(people_num)
            break
        else:
            print(err_msg)
    return people_num

def guess_number(guess_num,compter_num):
    """
    猜数字
    :param guess_num: 允许最多的猜数字的次数
    :param target_num: 要猜的目标数字
    :return:
    """
    #定义局部变量
    input_msg = "请输入1-10之间的整数："
    err_msg = "输入的不是整数！请重新输入！"
    for i in range(guess_num):
        #调用people_input_num方法
        people_num = people_input_num(input_msg,err_msg)
            #判断两个值是否相等
        if people_num == compter_num:
            print("猜对了")
            break
        else:
            #如果猜错了，前几次提示提醒信息
            if i != guess_num - 1:
                if people_num > compter_num:
                    print("猜大了")
                else:
                    print("猜小了")
            #最后一次机会还是猜错了，展示正确答案！
            else:
                print(f"猜错了，正确答案是：{compter_num}")
    print("游戏结束")


if __name__ == '__main__':
    compter_num = random.randint(1, 10)
    guess_num = 3
    #调用函数
    guess_number(guess_num,compter_num)